package com.zls.mapper;

import com.zls.pojo.Contractor;
import com.zls.pojo.Developer;
import com.zls.pojo.Project;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface ProjectMapper {
    @Select("Select * from project where status =1")
    List<Project> findAll();

    //分页查询
    //通过注解的方式实现映射
    //@Results 等价于 <ResultMap>标签
    //@Result  等价于<result>标签  column字段
    @Results(id = "projectMap", value = {
            @Result(column = "pid", property = "pid"),
            @Result(column = "pname", property = "pname"),
            @Result(column = "starttime", property = "starttime"),
            @Result(column = "endtime", property = "endtime"),
            @Result(column = "updatetime", property = "updatetime"),
            @Result(column = "did", property = "did"),
            @Result(column = "cid", property = "cid"),
            @Result(column = "did", property = "developer", javaType = Developer.class, one =
                    //FetchType.LAZY 懒加载   EAGER:立即加载
                    //select 代表的关联查询  调用指定的mapper方法
            @One(select = "com.com.zls.mapper.DeveloperMapper.findByDid", fetchType = FetchType.EAGER)),
            @Result(column = "cid", property = "contractor", javaType = Contractor.class, one =
            @One(select = "com.com.zls.mapper.ContractorMapper.findByCid", fetchType = FetchType.EAGER)),

    })
    @Select("<script>" +
            "select * from project where status =1" +
            "<if test='queryString!=null'> and pname like concat( '%',#{queryString},'%')" +
            "limit #{currentPage},#{pageSize}</if>" +
            "<if test='queryString==null'>limit #{currentPage},#{pageSize}</if> "+
            "</script>")
    List<Project> findPage(@Param("currentPage") Integer currentPage, @Param("pageSize") Integer pageSize, @Param("queryString") String queryString);

    //查询总记录数
    @Select("<script>" +
            "select count(*) from project where status =1" +
            "<if test='queryString!=null'> and pname like concat( '%',#{queryString},'%') </if>" +
            "</script>")
    Long count(String queryString);
    //增加
    @Insert("insert into project (pname,starttime,endtime,updatetime) values(#{pname},#{starttime},#{endtime},#{updatetime})")
    int addProject(Project project);

    //逻辑删除
    @Update("update project set status=0 where pid=#{pid}")
    int deleteProject(int pid);

    //根据pid查询
    @ResultMap("projectMap")
    @Select("select * from project where status =1 and pid=#{pid}")
    Project findByid(int pid);

    //修改
    @Update("update project set pname=#{pname},starttime=#{starttime},endtime=#{endtime},updatetime=#{updatetime} where pid=#{pid} and status =1")
    int updateProject(Project project);

    //绑定开发商
    @Update("update project set did=#{did} where pid =#{pid}")
    void bindDid(@Param("did") String did, @Param("pid") int pid);

    //绑定
    @Update("update project set cid=#{cid} where pid =#{pid}")
    void bindCid(@Param("cid") String cid, @Param("pid") int pid);

    //根据did查询工程信息
    @Select("select count(*) from project where status =1 and did=#{did}")
    int selectByDid(String did);

    //根据did查询工程集合
    @Select("select * from project where status =1 and did=#{did}")
    List<Project> selectByDids(String did);

    //根据cid查询工程信息
    @Select("select count(*) from project where status =1 and cid=#{cid}")
    int selectByCid(String cid);

    //根据cid查询工程集合
    @Select("select * from project where status =1 and cid=#{cid}")
    List<Project> selectByCids(String cid);

    //逻辑删除修改开发商did为空
    @Update("update project set did='' where status =1 and did =#{did}")
    int deleteByDid(String did);

    //逻辑删除修改承建商cid为空
    @Update("update project set cid='' where status =1 and cid =#{cid}")
    int deleteByCid(String cid);

}
